from Products.CMFCore.utils import getToolByName
from Products.CMFPlone.utils import base_hasattr
import urllib
class Login:
    def __init__(self,context,request):
        self.context = context
        self.request = request
    def __call__(self):
        descDict = {}
        if base_hasattr(self.request, '__ac_name') and base_hasattr(self.request, '__ac_password'):
            mt = getToolByName(self.context, 'portal_membership')
            acl_users = getToolByName(self.context, 'acl_users')
            if mt.isAnonymousUser(): # the user has not logged in
                descDict['Status'] = 'Error'
                descDict['Message'] = 'LoginError'
                self.request.RESPONSE.expireCookie('__ac', path='/')
            else:
                mt.loginUser(self.request)
                descDict['Status'] = 'Success'
                cookieAuth = self.request.RESPONSE.cookies['__ac']['value']
                cookieAuth = unicode(cookieAuth,"gbk")
                cookieAuth = cookieAuth.encode("utf-8")
                cookieAuth = urllib.quote(cookieAuth)
                descDict['Message'] =  '%s' % cookieAuth
        else:
            descDict['Status'] = 'Error'
            descDict['Message'] = 'LackData'

        result = "{\"Status\":\"%s\",\"Message\":\"%s\"}" % (descDict['Status'],descDict['Message'])
        return result